package com.fr.data;

import com.fr.base.ColumnRow;
import com.fr.base.Inter;
import com.fr.base.XMLable;
import com.fr.base.xml.XMLObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.core.db.DBUtils;
import com.fr.data.core.db.dml.Table;
import com.fr.data.impl.NameDatabaseConnection;
import com.fr.report.core.FormException;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.CharSetNameSpace;
import com.fr.report.write.config.ColumnConfig;
import com.fr.report.write.config.DMLConfig;
import com.fr.report.write.config.IntelliDMLConfig;
import java.sql.Connection;
import java.sql.SQLException;

public class DBManipulation
  implements XMLable
{
  private String dbName = null;
  private DMLConfig dmlConfig;

  public String getDBName()
  {
    return this.dbName;
  }

  public void setDBName(String paramString)
  {
    this.dbName = paramString;
  }

  public DMLConfig getDmlConfig()
  {
    return this.dmlConfig;
  }

  public void setDmlConfig(DMLConfig paramDMLConfig)
  {
    this.dmlConfig = paramDMLConfig;
  }

  public void commit2db(Calculator paramCalculator)
    throws FormException
  {
    new CommitWrapper(this, null).commit(paramCalculator, false);
  }

  public DBFeedback commit2dbAndFeedback(Calculator paramCalculator)
    throws FormException
  {
    CommitWrapper localCommitWrapper = new CommitWrapper(this, null);
    localCommitWrapper.commit(paramCalculator, true);
    return CommitWrapper.access$100(localCommitWrapper);
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Attributes").attr("dsName", getDBName()).end();
    ReportXMLUtils.writeXMLable(paramXMLPrintWriter, this.dmlConfig, "DMLConfig");
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str2 = paramXMLableReader.getTagName();
      if ("DMLConfig".equals(str2))
      {
        localObject = ReportXMLUtils.readDMLConfig(paramXMLableReader);
        setDmlConfig((DMLConfig)localObject);
      }
      else
      {
        String str1;
        if ("Attributes".equals(str2))
        {
          if ((str1 = paramXMLableReader.getAttr("dsName")) != null)
            setDBName(str1);
          localObject = null;
          String str3 = null;
          if ((str1 = paramXMLableReader.getAttr("schemaName")) != null)
            localObject = str1;
          if ((str1 = paramXMLableReader.getAttr("tableName")) != null)
            str3 = str1;
          if (str3 != null)
          {
            IntelliDMLConfig localIntelliDMLConfig1 = new IntelliDMLConfig();
            localIntelliDMLConfig1.setTable(new Table((String)localObject, str3));
            setDmlConfig(localIntelliDMLConfig1);
          }
        }
        else if ("Column".equals(str2))
        {
          localObject = null;
          boolean bool1 = false;
          boolean bool2 = false;
          if ((str1 = paramXMLableReader.getAttr("name")) != null)
            localObject = str1;
          if ((str1 = paramXMLableReader.getAttr("isKey")) != null)
            bool1 = Boolean.valueOf(str1).booleanValue();
          if ((str1 = paramXMLableReader.getAttr("skipUnmodified")) != null)
            bool2 = Boolean.valueOf(str1).booleanValue();
          DMLConfig localDMLConfig = getDmlConfig();
          if (localDMLConfig instanceof IntelliDMLConfig)
          {
            IntelliDMLConfig localIntelliDMLConfig2 = (IntelliDMLConfig)localDMLConfig;
            localIntelliDMLConfig2.addColumnConfig(new ColumnConfig((String)localObject, readColumnValue(paramXMLableReader), bool1, bool2));
          }
        }
      }
    }
  }

  public static Object readColumnValue(XMLableReader paramXMLableReader)
  {
    1 local1;
    paramXMLableReader.readXMLObject(local1 = new XMLObject("")
    {
      public void readXML(XMLableReader paramXMLableReader)
      {
        if (paramXMLableReader.isChildNode())
        {
          String str1 = paramXMLableReader.getTagName();
          if ("ColumnRow".endsWith(str1))
          {
            String str2;
            int i = 0;
            int j = 0;
            if ((str2 = paramXMLableReader.getAttr("column")) != null)
              i = Integer.parseInt(str2);
            if ((str2 = paramXMLableReader.getAttr("row")) != null)
              j = Integer.parseInt(str2);
            this.obj = ColumnRow.valueOf(i, j);
          }
          else if ("Parameter".equals(str1))
          {
            this.obj = ReportXMLUtils.readParameter(paramXMLableReader);
          }
          else if (("Object".equals(str1)) || ("O".equals(str1)))
          {
            this.obj = ReportXMLUtils.readObject(paramXMLableReader);
          }
        }
      }
    });
    return local1.getObject();
  }

  public String toString()
  {
    return ((this.dmlConfig == null) ? "" : this.dmlConfig.toString());
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    DBManipulation localDBManipulation = (DBManipulation)super.clone();
    if (this.dmlConfig != null)
      localDBManipulation.dmlConfig = ((DMLConfig)this.dmlConfig.clone());
    return localDBManipulation;
  }

  static DMLConfig access$200(DBManipulation paramDBManipulation)
  {
    return paramDBManipulation.dmlConfig;
  }

  private class CommitWrapper
  {
    private DBFeedback feedback;
    private final DBManipulation this$0;

    private CommitWrapper()
    {
      this.this$0 = paramDBManipulation;
    }

    public void commit(, boolean paramBoolean)
      throws FormException
    {
      if (DBManipulation.access$200(this.this$0) != null)
      {
        NameDatabaseConnection localNameDatabaseConnection = new NameDatabaseConnection(this.this$0.getDBName());
        Connection localConnection = null;
        try
        {
          localConnection = localNameDatabaseConnection.createConnection();
        }
        catch (Exception localException1)
        {
          DBUtils.closeConnection(localConnection);
          throw new FormException(Inter.getLocText("Utils-Can_not_create_connection") + ":" + this.this$0.getDBName() + "\n" + localException1.getMessage(), localException1);
        }
        try
        {
          localConnection.setAutoCommit(false);
        }
        catch (SQLException localSQLException)
        {
        }
        int i = ((com.fr.base.StringUtils.isNotBlank(localNameDatabaseConnection.getNewCharsetName())) && (com.fr.base.StringUtils.isNotBlank(localNameDatabaseConnection.getOriginalCharsetName())) && (!(com.fr.base.core.ComparatorUtils.equals(localNameDatabaseConnection.getNewCharsetName(), localNameDatabaseConnection.getOriginalCharsetName())))) ? 1 : 0;
        CharSetNameSpace localCharSetNameSpace = null;
        if (i != 0)
        {
          localCharSetNameSpace = new CharSetNameSpace(localNameDatabaseConnection.getNewCharsetName(), localNameDatabaseConnection.getOriginalCharsetName());
          paramCalculator.pushNameSpace(localCharSetNameSpace);
        }
        try
        {
          if (paramBoolean)
            this.feedback = DBManipulation.access$200(this.this$0).executeAndFeedback(localConnection, paramCalculator);
          else
            DBManipulation.access$200(this.this$0).execute(localConnection, paramCalculator);
          if (!(localConnection.getAutoCommit()))
            localConnection.commit();
        }
        catch (Exception localException2)
        {
        }
        finally
        {
          DBUtils.closeConnection(localConnection);
          if (i != 0)
            paramCalculator.removeNameSpace(localCharSetNameSpace);
        }
      }
    }

    CommitWrapper(, DBManipulation.1 param1)
    {
      this(paramDBManipulation);
    }

    static DBFeedback access$100(CommitWrapper paramCommitWrapper)
    {
      return paramCommitWrapper.feedback;
    }
  }
}